//Combines a texture with a blurred version using a depthmap to simulate a simple DOF effect.
float Distance;
float Range;
float Near;
float Far;

sampler SceneSampler : register(s0);

texture D1M;
sampler D1MSampler = sampler_state
{
   Texture = <D1M>;
   MinFilter = Point;
   MagFilter = Point;
   MipFilter = Point;  
   AddressU  = Clamp;
   AddressV  = Clamp;
};

texture BlurScene;
sampler BlurSceneSampler = sampler_state
{
   Texture = <BlurScene>;
   MinFilter = Linear;
   MagFilter = Linear;
   MipFilter = Linear;  
   AddressU  = Clamp;
   AddressV  = Clamp;
};
 
float4 PixelShaderFunction(float2 Tex: TEXCOORD0) : COLOR
{
	float4 NormalScene = tex2D(SceneSampler, Tex);
	float4 BlurScene = tex2D(BlurSceneSampler, Tex);
	float fDepth = 1-tex2D(D1MSampler, Tex).r;
	float f1 = (-Near * Far);
	float f2 = (fDepth - Far);
	float fSceneZ =  f1 / f2;
	float blurFactor = saturate(abs(fSceneZ-Distance)/Range);
	return lerp(NormalScene,BlurScene,blurFactor);
}

technique Technique1
{
    pass P0
    {
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}